﻿using NHibernate.Criterion;
using OfficeOpenXml;
using Online_Course.Core;
using Online_Course.Domain;
using Online_Course.Service;
using Online_Course.Web.Apps;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Online_Course.Web.Controllers
{
    public class Student_CourseController : Controller
    {
        //
        // GET: /Student_Course/
        #region 列表
        public ActionResult Index(int pageIndex = 1, string qryUserName = "")
        {
            //满足条件记录总数
            int count = 0;
            string Username = AppHelper.LoginedUser.Name;
            IList<Order> listcourse = new List<Order>() { new Order("ID", true) };
            IList<Student_Course> list = Container.Instance.Resolve<IStudent_CourseService>().Query(new List<ICriterion>(){Expression.Eq("TeacherName",Username)});
            //转换为PageList集合
            PageList<Student_Course> pageList = list.ToPageList<Student_Course>(
                pageIndex
                , PagerHelper.PageSize
                , count);
            return View(pageList);//用pageList集合填充页面。
        }
        #endregion

        #region 所有选课列表
        public ActionResult AllIndex(int pageIndex = 1, string qryUserName = "")
        {
            //满足条件记录总数
            int count = 0;
            IList<Order> listcourse = new List<Order>() { new Order("ID", true) };
            IList<Student_Course> list = Container.Instance.Resolve<IStudent_CourseService>().Query(new List<ICriterion>() { new LikeExpression("CourseName", "%" + qryUserName + "%") }, listcourse, pageIndex, PagerHelper.PageSize, out count);
            //转换为PageList集合
            PageList<Student_Course> pageList = list.ToPageList<Student_Course>(
                pageIndex
                , PagerHelper.PageSize
                , count);
            return View(pageList);//用pageList集合填充页面。
        }
        #endregion

        #region 审核列表
        public ActionResult Audit(int pageIndex = 1, string qryUserName = "")
        {
            //满足条件记录总数
            int count = 0;
            string Username = AppHelper.LoginedUser.Name;//获取当前登陆人的姓名
            IList<Order> listcourse = new List<Order>() { new Order("ID", true) };
            IList<Student_Course> list = Container.Instance.Resolve<IStudent_CourseService>().Query(new List<ICriterion>() { Expression.Eq("TeacherName", Username) });//查询选课学生名与登录用户名同等的学生用户
            //转换为PageList集合
            PageList<Student_Course> pageList = list.ToPageList<Student_Course>(
                  pageIndex
                  , PagerHelper.PageSize
                   , count);
            return View(pageList);//用pageList集合填充页面。
        }
        #endregion

        #region 审核结果
        public ActionResult StudentIndex(int pageIndex = 1, string qryUserName = "")
        {
            //满足条件记录总数
            int count = 0;
            string Username = AppHelper.LoginedUser.Name;//获取当前登陆人的姓名
            IList<Order> listcourse = new List<Order>() { new Order("ID", true) };
            IList<Student_Course> list = Container.Instance.Resolve<IStudent_CourseService>().Query(new List<ICriterion>() { Expression.Eq("StudentName", Username) } );//查询选课学生名与登录用户名同等的学生用户
            //转换为PageList集合
            PageList<Student_Course> pageList = list.ToPageList<Student_Course>(
                  pageIndex
                  , PagerHelper.PageSize
                   , count);
            return View(pageList);//用pageList集合填充页面。
        }
        #endregion

        #region 导出
        public ActionResult CorrectHomeWork(int id)
        {
            //string TableTitle = Session["TableTitle"].ToString();
            //string LName = Session["LName"].ToString();
            //string CName = Session["CName"].ToString();
            //int Sid=AppHelper.LoginedUser.ID;
            //SysUser user=Container.Instance.Resolve<ISysUserService>().GetEntity(id);
            //指定项目存放的路径
            string SwebFolder = "C:\\Users\\李逍遥\\Desktop";
            string Username = AppHelper.LoginedUser.Name;
            //string SwebFolder = "C:\\Users\\lyf\\Desktop";
            //指定项目名称
            string SfileName = Username + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
            //把项目名加到指定存放的路径
            FileInfo file = new FileInfo(Path.Combine(SwebFolder, SfileName));
            using (ExcelPackage package = new ExcelPackage(file))
            {
                //添加worksheet的名字
                ExcelWorksheet Worksheet = package.Workbook.Worksheets.Add("选课表单");
                //添加表头名字
                Worksheet.Cells[1, 1].Value = "学生名";
                Worksheet.Cells[1, 2].Value = "课程名";
                Worksheet.Cells[1, 3].Value = "课程人数";
                //添加值
                var RowNum = 2;
                //Student_Course SC = new Student_Course();
               // SC = Container.Instance.Resolve<IStudent_CourseService>().GetEntity(id);
                IList<Order> listOrder = new List<Order>() { new Order("ID", true) };
               // IList<QueryConditions> Query = new List<QueryConditions>();
               // Query.Add(new QueryConditions() { PropertyName = "TeacherName", Operator = "==", Value = "Username" });
                int Count = 0;
                IList<Student_Course> List = Container.Instance.Resolve<IStudent_CourseService>().Query(new List<ICriterion>() { Expression.Eq("TeacherName", Username) });
                //转换为pagelist集合
                PageList<Student_Course> PageList = List.ToPageList<Student_Course>(1, PagerHelper.PageSize, Count);
                foreach (var c in PageList)
                {
                    if (c.AuditActive==1)
                    {
                        Worksheet.Cells["A" + RowNum].Value = c.Student.Name;
                        Worksheet.Cells["B" + RowNum].Value = c.Course.Name;
                        Worksheet.Cells["C" + RowNum].Value = c.Course.ChoosePeople;
                        RowNum++;
                    }
                }
                package.Save();
            }
            //获取到存在项目中文件的路径
            var fileUrl = "C:\\Users\\李逍遥\\Desktop\\" + SfileName;
            //var fileUrl = "C:\\Users\\lyf\\Desktop\\" + SfileName;
            return File(fileUrl, "application/ms-excel", SfileName);
        }
        #endregion

        #region 全部导出
        public ActionResult AllCorrectHomeWork(int id)
        {
            //string TableTitle = Session["TableTitle"].ToString();
            //string LName = Session["LName"].ToString();
            //string CName = Session["CName"].ToString();
            //int Sid=AppHelper.LoginedUser.ID;
            //SysUser user=Container.Instance.Resolve<ISysUserService>().GetEntity(id);
            //指定项目存放的路径
            string SwebFolder = "C:\\Users\\李逍遥\\Desktop";
            string Username = AppHelper.LoginedUser.Name;
            //string SwebFolder = "C:\\Users\\lyf\\Desktop";
            //指定项目名称
            string SfileName = Username + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
            //把项目名加到指定存放的路径
            FileInfo file = new FileInfo(Path.Combine(SwebFolder, SfileName));
            using (ExcelPackage package = new ExcelPackage(file))
            {
                //添加worksheet的名字
                ExcelWorksheet Worksheet = package.Workbook.Worksheets.Add("选课表单");
                //添加表头名字
                Worksheet.Cells[1, 1].Value = "学生名";
                Worksheet.Cells[1, 2].Value = "课程名";
                Worksheet.Cells[1, 3].Value = "课程人数";
                //添加值
                var RowNum = 2;
                //Student_Course SC = new Student_Course();
                // SC = Container.Instance.Resolve<IStudent_CourseService>().GetEntity(id);
                IList<Order> listOrder = new List<Order>() { new Order("ID", true) };
                // IList<QueryConditions> Query = new List<QueryConditions>();
                // Query.Add(new QueryConditions() { PropertyName = "TeacherName", Operator = "==", Value = "Username" });
                int Count = 0;
                IList<Student_Course> List = Container.Instance.Resolve<IStudent_CourseService>().GetAll();
                //转换为pagelist集合
                PageList<Student_Course> PageList = List.ToPageList<Student_Course>(1, PagerHelper.PageSize, Count);
                foreach (var c in PageList)
                {
                    if (c.AuditActive == 1)
                    {
                        Worksheet.Cells["A" + RowNum].Value = c.Student.Name;
                        Worksheet.Cells["B" + RowNum].Value = c.Course.Name;
                        Worksheet.Cells["C" + RowNum].Value = c.Course.ChoosePeople;
                        RowNum++;
                    }
                }
                package.Save();
            }
            //获取到存在项目中文件的路径
            var fileUrl = "C:\\Users\\李逍遥\\Desktop\\" + SfileName;
            //var fileUrl = "C:\\Users\\lyf\\Desktop\\" + SfileName;
            return File(fileUrl, "application/ms-excel", SfileName);
        }
        #endregion

        #region 统计分析
        public ActionResult Analysis(int pageIndex = 1, string qryUserName = "")
        {
            //满足条件记录总数
            int count = 0;
            IList<Order> listcourse = new List<Order>() { new Order("ID", true) };
            IList<Student_Course> list = Container.Instance.Resolve<IStudent_CourseService>().Query(new List<ICriterion>() { new LikeExpression("CourseName", "%" + qryUserName + "%") }, listcourse, pageIndex, PagerHelper.PageSize, out count);
            //转换为PageList集合
            PageList<Student_Course> pageList = list.ToPageList<Student_Course>(
                pageIndex
                , PagerHelper.PageSize
                , count);
            return View(pageList);//用pageList集合填充页面。
        }
        #endregion

        #region 操作
        public ActionResult SwitchStatus(int id, string type)
        {
            int Count;
            Student_Course Scourse = Container.Instance.Resolve<IStudent_CourseService>().GetEntity(id);
            IList<Course> Cou = Container.Instance.Resolve<ICourseService>().GetAll();
            for (Count = 0; Count < Cou.Count; Count++)
            {
                if (Cou[Count].Name == Scourse.Course.Name)
                {
                    break;
                }
            }   
            Course Course = Container.Instance.Resolve<ICourseService>().GetEntity(Count + 1);
            if (type.Equals("Active") && Scourse.AuditActive == 0)
            {
                Scourse.AuditActive = 1;
                Course.ChoosePeople += 1;
            }
            else if (type.Equals("Enabled") && Scourse.AuditActive == 0)
            {
                Scourse.AuditActive = 2;
            }
            Container.Instance.Resolve<IStudent_CourseService>().Update(Scourse);
            Container.Instance.Resolve<ICourseService>().Update(Course);
            return RedirectToAction("Audit");//跳转到列表页面
        }

        public ActionResult Delete(int id, string type)
        {
            Student_Course Scourse = Container.Instance.Resolve<IStudent_CourseService>().GetEntity(id);
            Container.Instance.Resolve<IStudent_CourseService>().Delete(Scourse);
            return RedirectToAction("Audit");//跳转到列表页面
        }

        public ActionResult Continue(int id)
        {
            return RedirectToAction("../Course/StudentIndex");
        }
        #endregion
        //#region 导出
        //#endregion


        //导入Excel表
        //public ActionResult ExcelExport( int EvalBatch = 1)
        //{
        //    //指定项目存放的路径
        //    string sWebRootFolder = AppDomain.CurrentDomain.BaseDirectory;
        //    //string sWebRootFolder="D:\\";
        //    //指定项目名字
        //    //string sFileName = $"{Guid.NewGuid()}.xlsx";
        //    string sFileName = "报名信息表"+DateTime.Now.ToString("yyyyMMddHHmmss")+ ".xlsx";
        //    //把项目名加到指定存放的路径
        //    FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
        //    using (ExcelPackage package = new ExcelPackage(file))
        //    {
        //        //添加worksheet的名字
        //        ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("报名信息");
        //        //添加表头名字
        //        worksheet.Cells[1, 1].Value = "姓名";
        //        worksheet.Cells[1, 2].Value = "学号";
        //        worksheet.Cells[1, 3].Value = "电话";
        //        worksheet.Cells[1, 4].Value = "专业";
        //        worksheet.Cells[1, 5].Value = "级别层次";
        //        worksheet.Cells[1, 6].Value = "专业类型";
        //        worksheet.Cells[1, 7].Value = "资格名称";
        //        worksheet.Cells[1, 8].Value = "批次";
        //        //添加值
        //        var rowNum = 2; // rowNum 1 is head
        //        //var Homework = homeWorkService.Query(s => s.Task_Title == Task_Title && s.L_Name == L_Name);
        //        //var Homework = from s in con.Homework select s;//循环数据库输出数据

        //        EvalBatch eavl = new EvalBatch();
        //        eavl = Container.Instance.Resolve<IEvalBatchService>().GetEntity(EvalBatch);

        //        IList<Order> listOrder = new List<Order>() { new Order("DicTypeName", true) };
        //        IList<QueryConditions> query = new List<QueryConditions>();
        //        query.Add(new QueryConditions() { PropertyName = "IsActive", Operator = "=", Value = 1 });
        //        query.Add(new QueryConditions() { PropertyName = "EvalBatch", Operator = "=", Value = eavl.ID });
        //        int count=0;
        //        IList<Audit> list = Container.Instance.Resolve<IAuditService>().GetPaged(query, 1, PagerHelper.PageSize, out count);

        //        //转换为PageList集合
        //        PageList<Audit> pageList = list.ToPageList<Audit>(1  , PagerHelper.PageSize , count);
        //       // IList<Audit> list = Container.Instance.Resolve<IAuditService>().Query(new List<ICriterion>() { new LikeExpression("Name", "%" + qryUserName + "%") }, listOrder, pageIndex, PagerHelper.PageSize, out count);
        //        //var list = Container.Instance.Resolve<IAuditService>().Query(new List<IQueryable>(){query} );

        //        //list = list.OrderBy(s => s.S_Number);
        //        foreach (var message in pageList)
        //        {
        //            worksheet.Cells["A" + rowNum].Value = message.SysUser.Name;
        //            worksheet.Cells["B" + rowNum].Value = message.SysUser.Account;
        //            worksheet.Cells["C" + rowNum].Value = message.SysUser.Tel;
        //            worksheet.Cells["D" + rowNum].Value = message.SysUser.Major;
        //            worksheet.Cells["E" + rowNum].Value = message.DictionaryID.Name;
        //            worksheet.Cells["F" + rowNum].Value = message.DictionaryID.DicType;
        //            worksheet.Cells["G" + rowNum].Value = message.DictionaryID.DicTypeName;
        //            worksheet.Cells["H" + rowNum].Value = message.EvalBatch.Name;

        //            rowNum++;
        //        }
        //        package.Save();
        //    }
        //    //获取到存在项目中文件的路径
        //    var fileUrl = Server.MapPath("~/" + sFileName);
        //    //application/ms-excel为内部固定的，
        //    return File(fileUrl, "application/ms-excel", sFileName);
        //        }
        //    }
        //}

    }
}